const express = require('express')
const favicon = require('serve-favicon')
const path = require('path')
const fs = require('fs')
//创建vue实例
const Vue = require('vue')
//获取渲染器实例
const {createRenderer} = require('vue-server-renderer')
const renderer = createRenderer()

 //获取express实例
const server = express()
//处理favicon
server.use(favicon(path.join(__dirname,'../public','favicon.ico')))
//编写路由处理不同的url请求
server.get('*',(req,res)=>{
    //解析模板名称
    const template  = req.url.substr(1) || 'index'
    //加载模板
    const buffer = fs.readFileSync(path.join(__dirname,`${template}.html`))
    console.log(req.url)
    const app = new Vue({
        template:buffer.toString()//转换为模板字符串
    })
    renderer.renderToString(app).then(html=>{
        
        res.send(html)
    })
})
server.listen(8080)